/*     */ package com.zimbra.cs.dav.resource;
/*     */ 
/*     */ import com.google.common.base.Strings;
/*     */ import com.google.common.collect.Sets;
/*     */ import com.google.common.io.Closeables;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.calendar.ParsedDateTime;
/*     */ import com.zimbra.common.calendar.ParsedDuration;
/*     */ import com.zimbra.common.calendar.ZCalendar.ICalTok;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZCalendarBuilder;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZComponent;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZParameter;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZProperty;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZVCalendar;
/*     */ import com.zimbra.common.mime.shim.JavaMailInternetAddress;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.dav.DavContext;
/*     */ import com.zimbra.cs.dav.DavElements;
/*     */ import com.zimbra.cs.dav.DavException;
/*     */ import com.zimbra.cs.dav.caldav.CalDavUtils;
/*     */ import com.zimbra.cs.dav.service.DavResponse;
/*     */ import com.zimbra.cs.fb.FreeBusy;
/*     */ import com.zimbra.cs.fb.FreeBusy.Method;
/*     */ import com.zimbra.cs.fb.FreeBusyQuery;
/*     */ import com.zimbra.cs.index.SortBy;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailSender;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.Mountpoint;
/*     */ import com.zimbra.cs.mailbox.calendar.CalendarMailSender;
/*     */ import com.zimbra.cs.mailbox.calendar.FriendlyCalendaringDescription;
/*     */ import com.zimbra.cs.mailbox.calendar.Invite;
/*     */ import com.zimbra.cs.mailbox.calendar.ZAttendee;
/*     */ import com.zimbra.cs.mailbox.calendar.ZOrganizer;
/*     */ import com.zimbra.cs.service.FileUploadServlet.Upload;
/*     */ import com.zimbra.cs.util.AccountUtil;
/*     */ import com.zimbra.cs.util.AccountUtil.AccountAddressMatcher;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.text.ParseException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collection;
/*     */ import java.util.Date;
/*     */ import java.util.Enumeration;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Set;
/*     */ import javax.mail.Address;
/*     */ import javax.mail.internet.AddressException;
/*     */ import javax.mail.internet.InternetAddress;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import org.dom4j.Element;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ScheduleOutbox
/*     */   extends CalendarCollection
/*     */ {
/*     */   public ScheduleOutbox(DavContext ctxt, Folder f)
/*     */     throws DavException, ServiceException
/*     */   {
/*  78 */     super(ctxt, f);
/*  79 */     addResourceType(DavElements.E_SCHEDULE_OUTBOX);
/*     */   }
/*     */   
/*     */   public void handlePost(DavContext ctxt) throws DavException, IOException, ServiceException
/*     */   {
/*  84 */     DelegationInfo delegationInfo = new DelegationInfo(ctxt.getRequest().getHeader("Originator"));
/*  85 */     Enumeration<String> recipients = ctxt.getRequest().getHeaders("Recipient");
/*     */     
/*  87 */     InputStream in = ctxt.getUpload().getInputStream();
/*     */     
/*  89 */     ZCalendar.ZVCalendar vcalendar = ZCalendar.ZCalendarBuilder.build(in, "utf-8");
/*  90 */     Closeables.closeQuietly(in);
/*  91 */     Iterator<ZCalendar.ZComponent> iter = vcalendar.getComponentIterator();
/*  92 */     ZCalendar.ZComponent req = null;
/*  93 */     while (iter.hasNext()) {
/*  94 */       req = (ZCalendar.ZComponent)iter.next();
/*  95 */       if (req.getTok() != ZCalendar.ICalTok.VTIMEZONE)
/*     */         break;
/*  97 */       req = null;
/*     */     }
/*  99 */     if (req == null) {
/* 100 */       throw new DavException("empty request", 400);
/*     */     }
/* 102 */     ZimbraLog.dav.debug("originator: %s", new Object[] { delegationInfo.getOriginator() });
/*     */     
/* 104 */     boolean isVEventOrVTodo = (ZCalendar.ICalTok.VEVENT.equals(req.getTok())) || (ZCalendar.ICalTok.VTODO.equals(req.getTok()));
/* 105 */     boolean isOrganizerMethod = false;boolean isCancel = false;
/* 106 */     if (isVEventOrVTodo) {
/* 107 */       String method = vcalendar.getPropVal(ZCalendar.ICalTok.METHOD, null);
/* 108 */       if (method != null) {
/* 109 */         isOrganizerMethod = Invite.isOrganizerMethod(method);
/* 110 */         isCancel = ZCalendar.ICalTok.CANCEL.toString().equalsIgnoreCase(method);
/*     */       }
/*     */       
/* 113 */       CalDavUtils.removeAttendeeForOrganizer(req);
/*     */     }
/*     */     
/*     */ 
/* 117 */     ArrayList<String> attendees = new ArrayList();
/* 118 */     String organizer = null;
/* 119 */     for (Iterator<ZCalendar.ZProperty> propsIter = req.getPropertyIterator(); propsIter.hasNext();) {
/* 120 */       ZCalendar.ZProperty prop = (ZCalendar.ZProperty)propsIter.next();
/* 121 */       ZCalendar.ICalTok token = prop.getToken();
/* 122 */       if (ZCalendar.ICalTok.ATTENDEE.equals(token)) {
/* 123 */         String val = prop.getValue();
/* 124 */         if (val != null) {
/* 125 */           attendees.add(val.trim());
/*     */         }
/* 127 */       } else if (ZCalendar.ICalTok.ORGANIZER.equals(token)) {
/* 128 */         String val = prop.getValue();
/* 129 */         if (val != null) {
/* 130 */           organizer = val.trim();
/* 131 */           String addr = CalDavUtils.stripMailto(organizer);
/*     */           
/* 133 */           Account acct = Provisioning.getInstance().get(Key.AccountBy.name, addr);
/* 134 */           if (acct != null) {
/* 135 */             String newAddr = acct.getName();
/* 136 */             if (!addr.equals(newAddr)) {
/* 137 */               organizer = "mailto:" + newAddr;
/* 138 */               prop.setValue(organizer);
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */     AccountUtil.AccountAddressMatcher acctMatcher;
/*     */     
/* 147 */     if ((isVEventOrVTodo) && (delegationInfo.getOriginator() != null) && (ctxt.getAuthAccount() != null)) {
/* 148 */       acctMatcher = new AccountUtil.AccountAddressMatcher(ctxt.getAuthAccount());
/* 149 */       if (acctMatcher.matches(delegationInfo.getOriginatorEmail())) {
/* 150 */         if (isOrganizerMethod) {
/* 151 */           if (organizer != null) {
/* 152 */             String organizerEmail = CalDavUtils.stripMailto(organizer);
/* 153 */             if ((!organizerEmail.equalsIgnoreCase(delegationInfo.getOriginatorEmail())) && (acctMatcher.matches(organizerEmail)))
/*     */             {
/* 155 */               delegationInfo.setOriginator(organizer);
/* 156 */               ZimbraLog.dav.debug("changing originator to %s to match address/alias used in ORGANIZER", new Object[] { delegationInfo.getOriginator() });
/*     */             }
/*     */           }
/*     */         }
/*     */         else {
/* 161 */           for (String at : attendees) {
/* 162 */             String atEmail = CalDavUtils.stripMailto(at);
/* 163 */             if (delegationInfo.getOriginatorEmail().equalsIgnoreCase(atEmail))
/*     */               break;
/* 165 */             if (acctMatcher.matches(atEmail)) {
/* 166 */               delegationInfo.setOriginator(at);
/* 167 */               ZimbraLog.dav.debug("changing originator to %s to match address/alias used in ATTENDEE", new Object[] { delegationInfo.getOriginator() });
/*     */               
/* 169 */               break;
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 177 */     ArrayList<String> rcptArray = new ArrayList();
/* 178 */     while (recipients.hasMoreElements()) {
/* 179 */       String rcptHdr = (String)recipients.nextElement();
/* 180 */       String[] rcpts = null;
/* 181 */       if (rcptHdr.indexOf(',') > 0) {
/* 182 */         rcpts = rcptHdr.split(",");
/*     */       } else {
/* 184 */         rcpts = new String[] { rcptHdr };
/*     */       }
/* 186 */       for (String rcpt : rcpts) {
/* 187 */         if (rcpt != null) {
/* 188 */           rcpt = rcpt.trim();
/* 189 */           if (rcpt.length() != 0)
/*     */           {
/* 191 */             if (!rcpt.equalsIgnoreCase("invalid:nomail"))
/*     */             {
/*     */ 
/* 194 */               if (isVEventOrVTodo)
/*     */               {
/*     */ 
/* 197 */                 if ((isOrganizerMethod) && (rcpt.equalsIgnoreCase(organizer))) {
/*     */                   continue;
/*     */                 }
/*     */                 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 205 */                 if (isCancel) {
/* 206 */                   boolean isAttendee = false;
/*     */                   
/* 208 */                   for (String at : attendees) {
/* 209 */                     if (rcpt.equalsIgnoreCase(at)) {
/* 210 */                       isAttendee = true;
/* 211 */                       break;
/*     */                     }
/*     */                   }
/* 214 */                   if (!isAttendee) {
/* 215 */                     ZimbraLog.dav.info("Ignoring non-attendee recipient '%s' of CANCEL request; likely a client bug", new Object[] { rcpt });
/*     */                     
/*     */ 
/* 218 */                     continue;
/*     */                   }
/*     */                 }
/*     */               }
/*     */               
/* 223 */               rcptArray.add(rcpt);
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/* 229 */     Element scheduleResponse = ctxt.getDavResponse().getTop(DavElements.E_SCHEDULE_RESPONSE);
/* 230 */     for (String rcpt : rcptArray) {
/* 231 */       ZimbraLog.dav.debug("recipient email: " + rcpt);
/* 232 */       Element resp = scheduleResponse.addElement(DavElements.E_CALDAV_RESPONSE);
/* 233 */       switch (req.getTok()) {
/*     */       case VFREEBUSY: 
/* 235 */         handleFreebusyRequest(ctxt, req, delegationInfo.getOriginator(), rcpt, resp);
/* 236 */         break;
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */       case VEVENT: 
/* 243 */         if (isOrganizerMethod) {
/* 244 */           adjustOrganizer(ctxt, vcalendar, req, delegationInfo);
/*     */         }
/* 246 */         validateRequest(isOrganizerMethod, delegationInfo, organizer, ctxt, req);
/* 247 */         handleEventRequest(ctxt, vcalendar, req, delegationInfo, rcpt, resp);
/* 248 */         break;
/*     */       default: 
/* 250 */         throw new DavException("unrecognized request: " + req.getTok(), 400);
/*     */       }
/*     */       
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void validateRequest(boolean isOrganizerMethod, DelegationInfo delegationInfo, String organizer, DavContext ctxt, ZCalendar.ZComponent req)
/*     */     throws ServiceException, DavException
/*     */   {
/* 263 */     if ((!isOrganizerMethod) || ((organizer != null) && (organizer.equals(delegationInfo.getOriginator())))) {
/* 264 */       return;
/*     */     }
/*     */     
/* 267 */     AccountUtil.AccountAddressMatcher acctMatcher = new AccountUtil.AccountAddressMatcher(ctxt.getAuthAccount());
/* 268 */     if (!acctMatcher.matches(delegationInfo.getOriginatorEmail())) {
/* 269 */       throw new DavException(String.format("invalid POST to scheduling outbox '%s'. originator '%s' is not authorized account or ORGANIZER", new Object[] { ctxt.getRequest().getRequestURI(), delegationInfo.getOriginatorEmail() }), 400);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 274 */     String organizerEmail = CalDavUtils.stripMailto(organizer);
/* 275 */     Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(ctxt.getAuthAccount());
/* 276 */     List<Mountpoint> sharedCalendars = mbox.getCalendarMountpoints(ctxt.getOperationContext(), SortBy.NONE);
/*     */     
/* 278 */     if (sharedCalendars != null) {
/* 279 */       for (Mountpoint sharedCalendar : sharedCalendars) {
/* 280 */         Account acct = Provisioning.getInstance().get(Key.AccountBy.id, sharedCalendar.getOwnerId());
/* 281 */         if (acct != null) {
/* 282 */           acctMatcher = new AccountUtil.AccountAddressMatcher(acct);
/* 283 */           if (acctMatcher.matches(organizerEmail)) {
/* 284 */             return;
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 290 */     throw new DavException(String.format("invalid POST to scheduling outbox '%s'. '%s' cannot act as ORGANIZER '%s'", new Object[] { ctxt.getRequest().getRequestURI(), delegationInfo.getOriginatorEmail(), organizerEmail }), 400);
/*     */   }
/*     */   
/*     */ 
/*     */   private void handleFreebusyRequest(DavContext ctxt, ZCalendar.ZComponent vfreebusy, String originator, String rcpt, Element resp)
/*     */     throws DavException, ServiceException
/*     */   {
/* 297 */     ZCalendar.ZProperty dtstartProp = vfreebusy.getProperty(ZCalendar.ICalTok.DTSTART);
/* 298 */     ZCalendar.ZProperty dtendProp = vfreebusy.getProperty(ZCalendar.ICalTok.DTEND);
/* 299 */     ZCalendar.ZProperty durationProp = vfreebusy.getProperty(ZCalendar.ICalTok.DURATION);
/* 300 */     if ((dtstartProp == null) || ((dtendProp == null) && (durationProp == null)))
/* 301 */       throw new DavException("missing dtstart or dtend/duration in the schedule request", 400, null);
/*     */     long start;
/*     */     long end;
/* 304 */     try { ParsedDateTime startTime = ParsedDateTime.parseUtcOnly(dtstartProp.getValue());
/* 305 */       start = startTime.getUtcTime();
/* 306 */       long end; if (dtendProp != null) {
/* 307 */         end = ParsedDateTime.parseUtcOnly(dtendProp.getValue()).getUtcTime();
/*     */       } else {
/* 309 */         ParsedDuration dur = ParsedDuration.parse(durationProp.getValue());
/* 310 */         ParsedDateTime endTime = startTime.add(dur);
/* 311 */         end = endTime.getUtcTime();
/*     */       }
/*     */     } catch (ParseException pe) {
/* 314 */       throw new DavException("can't parse date", 400, pe);
/*     */     }
/*     */     
/* 317 */     ZimbraLog.dav.debug("rcpt: " + rcpt + ", start: " + new Date(start) + ", end: " + new Date(end));
/*     */     
/* 319 */     FreeBusy fb = null;
/* 320 */     if (ctxt.isFreebusyEnabled()) {
/* 321 */       FreeBusyQuery fbQuery = new FreeBusyQuery(ctxt.getRequest(), ctxt.getAuthAccount(), start, end, null);
/* 322 */       fbQuery.addEmailAddress(getAddressFromPrincipalURL(rcpt), -1);
/* 323 */       Collection<FreeBusy> fbResult = fbQuery.getResults();
/* 324 */       if (fbResult.size() > 0)
/* 325 */         fb = (FreeBusy)fbResult.iterator().next();
/*     */     }
/* 327 */     if (fb != null) {
/* 328 */       String fbMsg = fb.toVCalendar(FreeBusy.Method.REPLY, originator, rcpt, null);
/* 329 */       resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
/* 330 */       resp.addElement(DavElements.E_REQUEST_STATUS).setText("2.0;Success");
/* 331 */       resp.addElement(DavElements.E_CALENDAR_DATA).setText(fbMsg);
/*     */     } else {
/* 333 */       resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
/* 334 */       resp.addElement(DavElements.E_REQUEST_STATUS).setText("5.3;No f/b for the user");
/*     */     }
/*     */   }
/*     */   
/*     */   private void handleEventRequest(DavContext ctxt, ZCalendar.ZVCalendar cal, ZCalendar.ZComponent req, DelegationInfo delegationInfo, String rcpt, Element resp)
/*     */     throws ServiceException, DavException
/*     */   {
/* 341 */     if (!DavResource.isSchedulingEnabled()) {
/* 342 */       resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
/* 343 */       resp.addElement(DavElements.E_REQUEST_STATUS).setText("5.3;No scheduling for the user");
/* 344 */       return;
/*     */     }
/*     */     
/* 347 */     ArrayList<Address> recipients = new ArrayList();
/*     */     
/* 349 */     Account target = null;
/*     */     InternetAddress sender;
/* 351 */     InternetAddress from; try { sender = new JavaMailInternetAddress(delegationInfo.getOriginatorEmail());
/* 352 */       Provisioning prov = Provisioning.getInstance();
/* 353 */       if (ctxt.getActingAsDelegateFor() != null)
/* 354 */         target = prov.getAccountByName(ctxt.getActingAsDelegateFor());
/*     */       InternetAddress from;
/* 356 */       if (target != null) {
/* 357 */         from = AccountUtil.getFriendlyEmailAddress(target);
/*     */       } else { InternetAddress from;
/* 359 */         if (delegationInfo.getOwnerEmail() != null) {
/* 360 */           from = new JavaMailInternetAddress(delegationInfo.getOwnerEmail());
/*     */         } else {
/* 362 */           target = getMailbox(ctxt).getAccount();
/* 363 */           InternetAddress from; if (AccountUtil.addressMatchesAccount(target, delegationInfo.getOriginatorEmail()))
/*     */           {
/*     */ 
/* 366 */             from = sender;
/*     */           } else {
/* 368 */             from = AccountUtil.getFriendlyEmailAddress(target);
/*     */           }
/*     */         }
/*     */       }
/* 372 */       if ((sender.getAddress() != null) && (sender.getAddress().equalsIgnoreCase(from.getAddress()))) {
/* 373 */         sender = null;
/*     */       }
/* 375 */       InternetAddress to = new JavaMailInternetAddress(CalDavUtils.stripMailto(rcpt));
/* 376 */       recipients.add(to);
/*     */     } catch (AddressException e) {
/* 378 */       resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
/* 379 */       resp.addElement(DavElements.E_REQUEST_STATUS).setText("3.7;" + rcpt);
/* 380 */       return;
/*     */     }
/* 382 */     String status = req.getPropVal(ZCalendar.ICalTok.STATUS, "");
/* 383 */     String method = cal.getPropVal(ZCalendar.ICalTok.METHOD, "REQUEST");
/* 384 */     String subject = "";
/* 385 */     if (method.equals("REQUEST")) {
/* 386 */       ZCalendar.ZProperty organizerProp = req.getProperty(ZCalendar.ICalTok.ORGANIZER);
/* 387 */       if (organizerProp != null) {
/* 388 */         String organizerStr = getAddressFromPrincipalURL(new ZOrganizer(organizerProp).getAddress());
/* 389 */         if (!AccountUtil.addressMatchesAccount(getMailbox(ctxt).getAccount(), organizerStr)) {
/* 390 */           ZimbraLog.dav.debug("scheduling appointment on behalf of %s", new Object[] { organizerStr });
/*     */         }
/*     */       }
/* 393 */     } else if (method.equals("REPLY")) {
/* 394 */       ZCalendar.ZProperty attendeeProp = req.getProperty(ZCalendar.ICalTok.ATTENDEE);
/* 395 */       if (attendeeProp == null)
/* 396 */         throw new DavException("missing property ATTENDEE", 400);
/* 397 */       ZAttendee attendee = new ZAttendee(attendeeProp);
/* 398 */       String partStat = attendee.getPartStat();
/* 399 */       if (partStat.equals("AC")) {
/* 400 */         subject = "Accept: ";
/* 401 */       } else if (partStat.equals("TE")) {
/* 402 */         subject = "Tentative: ";
/* 403 */       } else if (partStat.equals("DE")) {
/* 404 */         subject = "Decline: ";
/*     */       }
/*     */     }
/*     */     
/* 408 */     if (status.equals("CANCELLED"))
/* 409 */       subject = "Cancelled: ";
/* 410 */     subject = subject + req.getPropVal(ZCalendar.ICalTok.SUMMARY, "");
/* 411 */     String uid = req.getPropVal(ZCalendar.ICalTok.UID, null);
/* 412 */     if (uid == null) {
/* 413 */       resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
/* 414 */       resp.addElement(DavElements.E_REQUEST_STATUS).setText("3.1;UID");
/* 415 */       return;
/*     */     }
/*     */     try {
/* 418 */       List<Invite> components = Invite.createFromCalendar(ctxt.getAuthAccount(), null, cal, false);
/* 419 */       FriendlyCalendaringDescription friendlyDesc = new FriendlyCalendaringDescription(components, ctxt.getAuthAccount());
/* 420 */       String desc = friendlyDesc.getAsPlainText();
/* 421 */       String descHtml = req.getDescriptionHtml();
/* 422 */       if ((descHtml == null) || (descHtml.length() == 0))
/* 423 */         descHtml = friendlyDesc.getAsHtml();
/* 424 */       Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(ctxt.getAuthAccount());
/* 425 */       MimeMessage mm = CalendarMailSender.createCalendarMessage(target, from, sender, recipients, subject, desc, descHtml, uid, cal);
/*     */       
/* 427 */       mbox.getMailSender().setSendPartial(true).sendMimeMessage(ctxt.getOperationContext(), mbox, Boolean.valueOf(true), mm, null, null, null, null, false);
/*     */     }
/*     */     catch (ServiceException e) {
/* 430 */       resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
/* 431 */       resp.addElement(DavElements.E_REQUEST_STATUS).setText("5.1");
/* 432 */       return;
/*     */     }
/* 434 */     resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
/* 435 */     resp.addElement(DavElements.E_REQUEST_STATUS).setText("2.0;Success");
/*     */   }
/*     */   
/*     */ 
/*     */   protected String getAddressFromPrincipalURL(String url)
/*     */     throws ServiceException, DavException
/*     */   {
/* 442 */     url = url.trim();
/* 443 */     if (url.startsWith("http://"))
/*     */     {
/*     */ 
/*     */ 
/* 447 */       int pos = url.indexOf("/service/dav/");
/* 448 */       if (pos != -1) {
/* 449 */         int start = pos + 13;
/* 450 */         int end = url.indexOf("/", start);
/* 451 */         String userId = end == -1 ? url.substring(start) : url.substring(start, end);
/* 452 */         Account organizer = Provisioning.getInstance().get(Key.AccountBy.name, userId);
/* 453 */         if (organizer == null)
/* 454 */           throw new DavException("user not found: " + userId, 400, null);
/* 455 */         return organizer.getName();
/*     */       }
/* 457 */     } else if (url.toLowerCase().startsWith("mailto:"))
/*     */     {
/* 459 */       while (url.toLowerCase().startsWith("mailto:")) {
/* 460 */         url = url.substring(7);
/*     */       }
/*     */     }
/* 463 */     return url;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void adjustOrganizer(DavContext ctxt, ZCalendar.ZVCalendar cal, ZCalendar.ZComponent req, DelegationInfo delegationInfo)
/*     */     throws ServiceException
/*     */   {
/* 478 */     Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(ctxt.getAuthAccount());
/* 479 */     String uid = req.getPropVal(ZCalendar.ICalTok.UID, null);
/* 480 */     CalendarItem matchingCalendarEntry = mbox.getCalendarItemByUid(ctxt.getOperationContext(), uid);
/* 481 */     if (matchingCalendarEntry == null) {
/* 482 */       List<Mountpoint> sharedCalendars = mbox.getCalendarMountpoints(ctxt.getOperationContext(), SortBy.NONE);
/*     */       
/* 484 */       if (sharedCalendars == null) {
/* 485 */         return;
/*     */       }
/* 487 */       Set<Account> accts = Sets.newHashSet();
/* 488 */       for (Mountpoint sharedCalendar : sharedCalendars) {
/* 489 */         accts.add(Provisioning.getInstance().get(Key.AccountBy.id, sharedCalendar.getOwnerId()));
/*     */       }
/* 491 */       for (Account acct : accts) {
/* 492 */         Mailbox sbox = MailboxManager.getInstance().getMailboxByAccount(acct);
/* 493 */         matchingCalendarEntry = sbox.getCalendarItemByUid(ctxt.getOperationContext(), uid);
/* 494 */         if (matchingCalendarEntry != null) {
/*     */           break;
/*     */         }
/*     */       }
/*     */     }
/* 499 */     if (matchingCalendarEntry == null) {
/* 500 */       return;
/*     */     }
/* 502 */     Invite[] invites = matchingCalendarEntry.getInvites();
/* 503 */     if (invites == null) {
/* 504 */       return;
/*     */     }
/* 506 */     for (Invite inv : invites) {
/* 507 */       ZOrganizer org = inv.getOrganizer();
/* 508 */       if (org != null) {
/* 509 */         delegationInfo.setOwner(org.getAddress());
/* 510 */         if (Strings.isNullOrEmpty(org.getCn())) {
/* 511 */           Account ownerAcct = Provisioning.getInstance().get(Key.AccountBy.name, org.getAddress());
/* 512 */           if (!Strings.isNullOrEmpty(ownerAcct.getDisplayName())) {
/* 513 */             delegationInfo.setOwnerCn(ownerAcct.getDisplayName());
/*     */           }
/* 515 */           break; }
/* 516 */         delegationInfo.setOwnerCn(org.getCn());
/*     */         
/* 518 */         break;
/*     */       }
/*     */     }
/* 521 */     if (delegationInfo.getOwner() == null) {
/* 522 */       return;
/*     */     }
/* 524 */     AccountUtil.AccountAddressMatcher acctMatcher = new AccountUtil.AccountAddressMatcher(ctxt.getAuthAccount());
/* 525 */     boolean originatorIsCalEntryOrganizer = acctMatcher.matches(delegationInfo.getOwnerEmail());
/* 526 */     if (originatorIsCalEntryOrganizer) {
/* 527 */       return;
/*     */     }
/* 529 */     for (ZCalendar.ZComponent component : cal.getComponents()) {
/* 530 */       ZCalendar.ZProperty organizerProp = component.getProperty(ZCalendar.ICalTok.ORGANIZER);
/* 531 */       if (organizerProp != null) {
/* 532 */         organizerProp.setValue(delegationInfo.getOwner());
/* 533 */         ZCalendar.ZParameter cn = organizerProp.getParameter(ZCalendar.ICalTok.CN);
/* 534 */         if (cn == null) {
/* 535 */           organizerProp.addParameter(new ZCalendar.ZParameter(ZCalendar.ICalTok.CN, delegationInfo.getOwnerCn()));
/*     */         } else {
/* 537 */           cn.setValue(delegationInfo.getOwnerCn());
/*     */         }
/* 539 */         ZCalendar.ZParameter sentBy = organizerProp.getParameter(ZCalendar.ICalTok.SENT_BY);
/* 540 */         if (sentBy == null) {
/* 541 */           organizerProp.addParameter(new ZCalendar.ZParameter(ZCalendar.ICalTok.SENT_BY, delegationInfo.getOriginator()));
/*     */         } else {
/* 543 */           sentBy.setValue(delegationInfo.getOriginator());
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   private class DelegationInfo
/*     */   {
/*     */     private String originator;
/*     */     private String originatorEmail;
/*     */     private String owner;
/*     */     private String ownerEmail;
/*     */     private String ownerCn;
/*     */     
/* 558 */     DelegationInfo(String originator) { setOriginator(originator); }
/*     */     
/* 560 */     String getOriginator() { return this.originator; }
/*     */     
/* 562 */     void setOriginator(String originator) { this.originator = originator;
/* 563 */       this.originatorEmail = CalDavUtils.stripMailto(originator); }
/*     */     
/* 565 */     String getOriginatorEmail() { return this.originatorEmail; }
/* 566 */     String getOwner() { return this.owner; }
/*     */     
/* 568 */     void setOwner(String owner) { this.owner = owner;
/* 569 */       this.ownerEmail = CalDavUtils.stripMailto(owner);
/*     */     }
/*     */     
/* 572 */     String getOwnerEmail() { return this.ownerEmail; }
/*     */     
/* 574 */     String getOwnerCn() { return Strings.isNullOrEmpty(this.ownerCn) ? this.owner : this.ownerCn; }
/* 575 */     void setOwnerCn(String ownerCn) { this.ownerCn = ownerCn; }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/resource/ScheduleOutbox.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */